package net.maku.kyy.security.mobile;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.maku.framework.common.constant.MessageConstant;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.security.crypto.Sm2Util;
import net.maku.framework.security.user.UserDetail;
import net.maku.kyy.dao.KyyUserDao;
import net.maku.kyy.entity.KyyUserEntity;
import net.maku.kyy.service.KyyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
public class KyyAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private KyyUserDao kyyUserDao;
    
    @Autowired
    private KyyUserDetailsService kyyUserDetailsService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String accountOrPhone = authentication.getName();
        String password = authentication.getCredentials().toString();
        //判断是手机号存在
        Boolean existPhone = isExistPhone(accountOrPhone);
        KyyUserEntity kyyUserEntity;
        if (existPhone) {
            //根据手机号获取用户信息
            kyyUserEntity = kyyUserDao.selectOne(new QueryWrapper<KyyUserEntity>().eq("mobile", accountOrPhone));
        }else {
            //根据账号获取用户信息
            kyyUserEntity = kyyUserDao.selectOne(new QueryWrapper<KyyUserEntity>().eq("account", accountOrPhone));
        }

        //判断用户是否存在
        if (kyyUserEntity == null) {
            throw new ServerException(MessageConstant.ACCOUNT_NOT_EXIST);
        }

        //验证密码
        String kyyPassword = Sm2Util.decrypt(kyyUserEntity.getPassword());
        if (!StrUtil.equals(password, kyyPassword)) {
            throw new ServerException(MessageConstant.PASSWORD_ERROR);
        }

        // 创建用户详情对象
        UserDetail userDetail = BeanUtil.copyProperties(kyyUserEntity, UserDetail.class);




        // 通过KyyUserDetailsService设置用户权限
        userDetail = (UserDetail) kyyUserDetailsService.getUserDetails(userDetail);
        
        // 返回认证成功的Authentication对象
        return new UsernamePasswordAuthenticationToken(userDetail, null, userDetail.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
    }

    /**
     * 判断手机号是否存在
     *
     * @param phone
     * @return
     */
    private Boolean isExistPhone(String phone) {
        //根据手机号判断账号是否存在
        boolean exists = kyyUserDao.exists(new QueryWrapper<KyyUserEntity>().eq("mobile", phone));
        return exists;
    }
} 